using System;
using System.Collections.Generic;
using System.Text;

namespace IGGCore
{
    public class Statistics
    {
        private string m_avg;
        private string m_hr;
        private string m_rbi;
        private string m_stolenBase;
        private string m_sluggingPercent;
        private string m_obp;
        private string m_runs;
        private string m_shutouts;
        private string m_hits;
        private string m_bb;
        private string m_double;
        private string m_triple;
        private string m_totalBases;
        private string m_era;
        private string m_wonLost;
        private string m_gamesPitched;
        private string m_saves;
        private string m_completeGames;
        private string m_innings;
        private string m_strikeouts;

        private string m_dailyAVG;
        private string m_dailyHR;
        private string m_dailyRBI;
        private string m_dailyERA;
        private string m_dailyWonLost;

        public string DailyWonLost
        {
            get { return m_dailyWonLost; }
            set { m_dailyWonLost = value; }
        }

        public string DailyERA
        {
            get { return m_dailyERA; }
            set { m_dailyERA = value; }
        }

        public string DailyRBI
        {
            get { return m_dailyRBI; }
            set { m_dailyRBI = value; }
        }

        public string DailyHR
        {
            get { return m_dailyHR; }
            set { m_dailyHR = value; }
        }

        public string DailyAVG
        {
            get { return m_dailyAVG; }
            set { m_dailyAVG = value; }
        }


        public string SluggingPercent
        {
            get { return m_sluggingPercent; }
            set { m_sluggingPercent = value; }
        }
        
        public string OBP
        {
            get { return m_obp; }
            set { m_obp = value; }
        }
        
        public string Runs
        {
            get { return m_runs; }
            set { m_runs = value; }
        }
        
        public string Hits
        {
            get { return m_hits; }
            set { m_hits = value; }
        }

        public string BaseOnBalls
        {
            get { return m_bb; }
            set { m_bb = value; }
        }

        public string Doubles
        {
            get { return m_double; }
            set { m_double = value; }
        }

        public string Triples
        {
            get { return m_triple; }
            set { m_triple = value; }
        }
        
        public string TotalBases
        {
            get { return m_totalBases; }
            set { m_totalBases = value; }
        }
        
        public string ERA
        {
            get { return m_era; }
            set { m_era = value; }
        }

        public string WonLost
        {
            get { return m_wonLost; }
            set { m_wonLost = value; }
        }
        
        public string GamesPitched
        {
            get { return m_gamesPitched; }
            set { m_gamesPitched = value; }
        }
        
        public string Saves
        {
            get { return m_saves; }
            set { m_saves = value; }
        }
        
        public string Innings
        {
            get { return m_innings; }
            set { m_innings = value; }
        }
 
        public string Strikeouts
        {
            get { return m_strikeouts; }
            set { m_strikeouts = value; }
        }
 
        public string CompleteGames
        {
            get { return m_completeGames; }
            set { m_completeGames = value; }
        }
   
        public string Shutouts
        {
            get { return m_shutouts; }
            set { m_shutouts = value; }
        }

        public string Avg
        {
            get { return m_avg; }
            set { m_avg = value; }
        }

        public string HR
        {
            get { return m_hr; }
            set { m_hr = value; }
        }

        public string RBI
        {
            get { return m_rbi; }
            set { m_rbi = value; }
        }

        public string StolenBases
        {
            get { return m_stolenBase; }
            set { m_stolenBase = value; }
        }


        private string extract(string raw, string start, string end)
        {
            int idxStart = raw.IndexOf(start) + start.Length;
            if (end != null)
            {
                int idxEnd = raw.IndexOf(end);
                return raw.Substring(idxStart, idxEnd - idxStart);
            }
            else
            {
                return raw.Substring(idxStart);
            }
        }


        public void ProcessRawStats(string nlraw, string alraw)
        {

            // AVG
            Avg = GetStatsContent(nlraw, alraw, "AVG", "HOME", 5, "AVG");
            // Daily AVG
            DailyAVG = GetDailyStatsContent(nlraw, alraw, "AVG", "HOME", 5, "AVG");

            // HR
            HR = GetStatsContent(nlraw, alraw, "HOME RUNS", "RUNS BATTED IN", 3, "HOME RUNS");
            // Daily HR
            DailyHR = GetDailyStatsContent(nlraw, alraw, "HOME RUNS", "RUNS BATTED IN", 3, "HOME RUNS");

            // RBI
            RBI = GetStatsContent(nlraw, alraw, "RUNS BATTED IN", "STOLEN BASES", 3, "RBI");
            // Daily RBI
            DailyRBI = GetDailyStatsContent(nlraw, alraw, "RUNS BATTED IN", "STOLEN BASES", 3, "RBI");

            // STOLEN BASES
            StolenBases = GetStatsContent(nlraw, alraw, "STOLEN BASES", "SLUGGING PERCENTAGE", 3, "STOLEN BASES");
            // SLUGGING PERCENT
            SluggingPercent = GetStatsContent(nlraw, alraw, "SLUGGING PERCENTAGE", "ON-BASE PERCENTAGE", 5, "SLUGGING %");
            // ON-BASE PERCENTAGE
            OBP = GetStatsContent(nlraw, alraw, "ON-BASE PERCENTAGE", "RUNS SCORED", 5, "OBP");
            // RUNS SCORED
            Runs = GetStatsContent(nlraw, alraw, "RUNS SCORED", "HITS", 4, "RUNS SCORED");
            // HITS
            Hits = GetStatsContent(nlraw, alraw, "HITS", "BASES ON BALLS", 4, "HITS");
            // BASES ON BALLS
            BaseOnBalls = GetStatsContent(nlraw, alraw, "BASES ON BALLS", "DOUBLES", 4, "BASE ON BALLS");
            // DOUBLES
            Doubles = GetStatsContent(nlraw, alraw, "DOUBLES", "TRIPLES", 3, "DOUBLES");
            // TRIPLES
            Triples = GetStatsContent(nlraw, alraw, "TRIPLES", "TOTAL BASES", 3, "TRIPLES");
            // TOTAL BASES
            TotalBases = GetStatsContent(nlraw, alraw, "TOTAL BASES", "EARNED RUN AVERAGE", 4, "TOTAL BASES");
            
            // EARNED RUN AVERAGE
            ERA = GetStatsContent(nlraw, alraw, "EARNED RUN AVERAGE", "WON-LOST", 5, "ERA");
            // DAILY EARNED RUN AVERAGE
            DailyERA = GetDailyStatsContent(nlraw, alraw, "EARNED RUN AVERAGE", "WON-LOST", 5, "ERA");

            // WON-LOST
            WonLost = GetStatsContent(nlraw, alraw, "WON-LOST", "GAMES PITCHED", 12, "WON - LOST");
            // DAILY WON-LOST
            DailyWonLost = GetDailyStatsContent(nlraw, alraw, "WON-LOST", "GAMES PITCHED", 12, "WON - LOST");

            // GAMES PITCHED
            GamesPitched = GetStatsContent(nlraw, alraw, "GAMES PITCHED", "SAVES", 4, "GAMES PITCHED");
            // SAVES
            Saves = GetStatsContent(nlraw, alraw, "SAVES", "INNINGS PITCHED", 3, "SAVES");
            // INNINGS PITCHED
            Innings = GetStatsContent(nlraw, alraw, "INNINGS PITCHED", "STRIKEOUTS", 4, "INNINGS PITCHED");
            // STRIKEOUTS
            Strikeouts = GetStatsContent(nlraw, alraw, "STRIKEOUTS", "COMPLETE GAMES", 4, "STRIKEOUTS");
            // COMPLETE GAMES
            CompleteGames = GetStatsContent(nlraw, alraw, "COMPLETE GAMES", "SHUTOUTS", 3, "COMPLETE GAMES");
            // SHUTOUTS
            Shutouts = GetStatsContent(nlraw, alraw, "SHUTOUTS", null, 3, "SHUTOUTS");
        }

        private string GetStatsContent(string nlraw, string alraw, string start, string end, int valueLen, string header)
        {
            string hrNL = extract(nlraw, start, end);
            string[] linesNL = hrNL.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            string hrAL = extract(alraw, start, end);
            string[] linesAL = hrAL.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int minLen = linesNL.Length;
            if (linesAL.Length < minLen)
            {
                minLen = linesAL.Length;
            }
            string[] newlines = new string[minLen];

            for (int i = 0; i < minLen; i++)
            {
                int idxNL = linesNL[i].IndexOf("  ");
                int idxAL = linesAL[i].IndexOf("  ");
                string nameNL = "None.";
                string nameAL = "None.";
                string avgcellNL = "";
                string avgcellAL = "";

                if (idxNL > -1)
                {
                    nameNL = linesNL[i].Substring(0, idxNL).Trim();
                    avgcellNL = linesNL[i].Substring(linesNL[i].Length - valueLen).Trim();
                }
                if (idxAL > -1)
                {
                    nameAL = linesAL[i].Substring(0, idxAL).Trim(); 
                    avgcellAL = linesAL[i].Substring(linesAL[i].Length - valueLen).Trim();
                }

                if (header.ToUpper().Contains("WON"))
                {
                    if (avgcellNL.Contains("   "))
                    {
                        string wins = avgcellNL.Substring(0, avgcellNL.IndexOf("   ")).Trim();
                        string losses = avgcellNL.Substring(avgcellNL.Length - 3).Trim();
                        avgcellNL = ToolBox.GetFormattedStringPadRight(wins, 3) + losses;
                    }
                    if (avgcellAL.Contains("   "))
                    {
                        string wins = avgcellAL.Substring(0, avgcellAL.IndexOf("   ")).Trim();
                        string losses = avgcellAL.Substring(avgcellAL.Length - 3).Trim();
                        avgcellAL = ToolBox.GetFormattedStringPadRight(wins, 3) + losses;
                    }
                }

                newlines[i] = " "
                    + ToolBox.GetFormattedStringPadRight(nameNL, 23)
                    + ToolBox.GetFormattedStringPadRight(avgcellNL, 16)
                    + ToolBox.GetFormattedStringPadRight(nameAL, 23)
                    + avgcellAL
                    + "\r\n";
            }
            string content = " " + ToolBox.GetFormattedStringPadRight("NL " + header, 39) + "AL " + header + "\r\n";
            content += String.Join("", newlines);
            return content;
        }

        private string GetDailyStatsContent(string nlraw, string alraw, string start, string end, int valueLen, string header)
        {
            string hrNL = extract(nlraw, start, end);
            string[] linesNL = hrNL.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            string hrAL = extract(alraw, start, end);
            string[] linesAL = hrAL.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int minLen = linesNL.Length;
            if (linesAL.Length < minLen)
            {
                minLen = linesAL.Length;
            }
            if (minLen > 3)
            {
                minLen = 3;
            }
            string[] newlines = new string[minLen];

            for (int i = 0; i < minLen; i++)
            {
                int idxNL = linesNL[i].IndexOf("  ");
                int idxAL = linesAL[i].IndexOf("  ");
                string nameNL = "None.";
                string nameAL = "None.";
                string avgcellNL = "";
                string avgcellAL = "";

                if (idxNL > -1)
                {
                    nameNL = linesNL[i].Substring(0, idxNL).Trim();
                    avgcellNL = linesNL[i].Substring(linesNL[i].Length - valueLen).Trim();
                }
                if (idxAL > -1)
                {
                    nameAL = linesAL[i].Substring(0, idxAL).Trim();
                    avgcellAL = linesAL[i].Substring(linesAL[i].Length - valueLen).Trim();
                }

                if (header.ToUpper().Contains("WON"))
                {
                    if (avgcellNL.Contains("   "))
                    {
                        string wins = avgcellNL.Substring(0, avgcellNL.IndexOf("   ")).Trim();
                        string losses = avgcellNL.Substring(avgcellNL.Length - 3).Trim();
                        avgcellNL = ToolBox.GetFormattedStringPadRight(wins, 3) + losses;
                    }
                    if (avgcellAL.Contains("   "))
                    {
                        string wins = avgcellAL.Substring(0, avgcellAL.IndexOf("   ")).Trim();
                        string losses = avgcellAL.Substring(avgcellAL.Length - 3).Trim();
                        avgcellAL = ToolBox.GetFormattedStringPadRight(wins, 3) + losses;
                    }
                }

                newlines[i] = " "
                    + ToolBox.GetFormattedStringPadRight(nameNL, 18)
                    + ToolBox.GetFormattedStringPadRight(avgcellNL, 11)
                    + ToolBox.GetFormattedStringPadRight(nameAL, 18)
                    + avgcellAL
                    + "\r\n";
            }
            string content = " " + ToolBox.GetFormattedStringPadRight("NL " + header, 29) + "AL " + header + "\r\n";
            content += String.Join("", newlines);
            return content;
        }
    }
}
